package data;

public class BasicArray<E> {
    // 数组中的数据大小
    private int size = 0;
    // 数组数据
    private E[] data;
    // 容量，空间大小
    private int capacity = 0;

    public BasicArray(){
        this.size = 0;
        this.capacity = 10;
        this.data = (E[]) new Object[this.capacity] ;
    }

    public BasicArray(int capacity){
        this.size = 0;
        this.capacity = 10;
        this.data = (E[]) new Object[this.capacity] ;
    }

    // 数据在指定位置插入数据，该位置后的所有数据右移
    public void add(int index,E e){
        // 校验
        if(index < 0 || index > this.capacity){
            throw new IllegalArgumentException("index value error ");
        }

        for (int i = this.getSize(); i > index; i--) {
            E historyValue = this.data[i-1];
            this.data[i] = historyValue;
        }
        this.data[index] = e;
        this.size ++;
    }

    public void addFirst(E e){
        this.add(0,e);
    }

    public void addLast(E e){
        this.add(this.getSize()-1,e);
    }

    // 删除则是要将数据向左移
    public void remove(int index){
        // 校验
        if(index < 0 || index > this.capacity){
            throw new IllegalArgumentException("index value error ");
        }

        for (int i = index; i < this.getSize(); i++) {
            E nextValue = this.data[i+1];
            this.data[i] = nextValue;
        }

        this.size --;
    }

    public void removeFirst(){
        this.remove(0);
    }

    public void removeLast(){
        this.remove(this.size);
    }

    public void set(int index,E e){
        this.data[index] = e;
    }

    public E get(int index){
        return this.data[index];
    }

    public boolean contains(E e){
        for (int i = 0; i < size; i++) {
            if(this.data[i].equals(e)){
                return true;
            }
        }
        return false;
    }

    public int find(E e){
        for (int i = 0; i < size; i++) {
            if(this.data[i].equals(e)){
                return i;
            }
        }
        return -1;
    }

    public int getSize(){
        return this.size;
    }
    // 取出所有数据
    public E[] getData(){
        return this.data;
    }


}
